﻿Imports RegExUtility.ViewModel

Public Class ViewCommandFactory
    Implements ICommandFactory

#Region "Events"
    ' Insert Event Declarations Here
#End Region

#Region "Properties"
#Region "Constants"
    ' Insert Constant Declarations Here
#End Region

    Public Shared ReadOnly Property Instance As ViewCommandFactory
        Get
            Static l_instance As New ViewCommandFactory()
            Return l_instance
        End Get
    End Property
#End Region

#Region "Constructors"
    Private Sub New()
    End Sub
#End Region

#Region "Methods"
    Public Function CreateCommand(ByVal executeCallback As Action(Of Object), ByVal executable As Boolean) As IViewModelCommand Implements ICommandFactory.CreateCommand
        Return New ViewCommand(executeCallback, executable)
    End Function

#Region "Event Raiser Methods"
    ' Insert Method Declarations which Raise an Event Here
#End Region

#Region "Event Handlers"
    ' Insert Method Declarations which Handle an Event Here
#End Region
#End Region

    Private Class ViewCommand
        Implements ICommand
        Implements IViewModelCommand

#Region "Events"
        Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged
#End Region

#Region "Properties"
#Region "Constants"
        ' Insert Constant Declarations Here
#End Region

        Private _executeCallbackReference As Action(Of Object)
        Private Property ExecuteCallback As Action(Of Object)
            Get
                Return _executeCallbackReference
            End Get
            Set(ByVal value As Action(Of Object))
                _executeCallbackReference = value
            End Set
        End Property

        Private _executable As Boolean
        Public Property Executable As Boolean Implements IViewModelCommand.Executable
            Get
                Return _executable
            End Get
            Set(ByVal value As Boolean)
                If _executable <> value Then
                    _executable = value
                    RaiseEvent CanExecuteChanged(sender:=Me, e:=EventArgs.Empty)
                End If
            End Set
        End Property
#End Region

#Region "Constructors"
        Public Sub New(ByVal executeCallback As Action(Of Object), ByVal executable As Boolean)
            Me.ExecuteCallback = executeCallback
            Me.Executable = executable
        End Sub
#End Region

#Region "Methods"
        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
            Return Me.Executable
        End Function

        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute, IViewModelCommand.Execute
            ExecuteCallback.Invoke(parameter)
        End Sub

#Region "Event Raiser Methods"
        ' Insert Method Declarations which Raise an Event Here
#End Region

#Region "Event Handlers"
        ' Insert Method Declarations which Handle an Event Here
#End Region
#End Region

    End Class

End Class
